package com.hujia.hjsurprisemeal.controller;

import com.hujia.hjsurprisemeal.model.User;
import com.hujia.hjsurprisemeal.service.UserService;
import com.hujia.hjsurprisemeal.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users") // 修改基础路径为 /users
@Validated // 开启验证
public class UserController {

    @Autowired
    private UserService userService;

    // 创建用户的 API (POST 请求)
    @PostMapping("/register")
    @Operation(summary = "注册用户", description = "创建新用户")
    public ResponseEntity<Result<User>> register(@RequestBody User user) {
        try {
            User registeredUser = userService.register(user.getName(), user.getEmail(), user.getPassword());
            return ResponseEntity.ok(Result.success(registeredUser));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Result.error("注册失败：" + e.getMessage()));
        }
    }

    // 获取用户详情的 API (GET 请求)
    @GetMapping("/findByname/{name}")
    @Operation(summary = "获取用户详情", description = "获取指定用户的详细信息")
    public ResponseEntity<Result<User>> getUserByName(@PathVariable String name) {
        try {
            User user = userService.getUserByName(name); // 使用用户名查询用户
            return ResponseEntity.ok(Result.success(user));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Result.error("获取用户详情失败：" + e.getMessage()));
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息", description = "根据用户ID更新用户的姓名、邮箱和密码")
    public Result<User> updateUser(@RequestParam Long userId,
                                   @RequestParam String name,
                                   @RequestParam String email,
                                   @RequestParam String password) {
        boolean updated = userService.updateUser(userId, name, email, password);
        User user = userService.findByName(name);
        if (updated) {
            return Result.success(user);
        } else {
            return Result.error("用户不存在或更新失败");
        }
    }

    // 删除用户的 API (DELETE 请求)
    @DeleteMapping("/delete")
    @Operation(summary = "删除用户", description = "根据用户 ID 删除用户")
    public ResponseEntity<Result<String>> deleteUser(@RequestParam int id) {
        try {
            int deleteResult = userService.deleteUser(id);
            if (deleteResult > 0) {
                return ResponseEntity.ok(Result.success("删除成功"));
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Result.error("删除失败"));
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Result.error("删除失败：" + e.getMessage()));
        }
    }
}